<template>
    <div class="on-sale-good-car">
        <div class="search-content">
            <el-input class="search-content-input" v-model="searchContent.id" placeholder="请输入ID"></el-input>
            <el-input class="search-content-input" v-model="searchContent.address" placeholder="请输入预约上牌点"></el-input>
			<div class="putaway-time-wrapper">
                <span class="putaway-time-label">预约时间：</span>
                <el-date-picker
                    v-model="searchContent.arrive_time"
					value-format="yyyy-MM-dd"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
            </div>
            <el-input class="search-content-input" v-model="searchContent.mobile" placeholder="预约客户手机号"></el-input>
            
            <div class="putaway-time-wrapper">
                <span class="putaway-time-label">创建日期：</span>
                <el-date-picker
                    v-model="searchContent.add_time"
					value-format="yyyy-MM-dd"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
            </div>
			<el-select v-model="searchContent.originate" placeholder="预约来源" v-if="!externalData">
                <el-option
                    v-for="item in originateOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
			<el-select v-model="searchContent.state" placeholder="预约状态">
                <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button type="primary" @click="resetSearchData">重置</el-button>
        </div>
        <div class="search-list">
            <el-table :data="goodsList" border :height="oHeight" header-cell-class-name="hdCellStyle">
				<el-table-column
					align="center"
					type="selection"
					width="55">
				</el-table-column>
                <el-table-column
                    align="center"
                    prop="id"
                    label="ID"
                    min-width="100"></el-table-column>
                <el-table-column
                    align="center"
                    prop="address"
                    label="预约上牌点"
                    min-width="160"></el-table-column>
                <el-table-column
                    align="center"
                    prop="store_id"
                    label="店铺ID"
                    min-width="80">
				</el-table-column>					
                <el-table-column
                    align="center"
                    prop="licensing_time"
                    label="预约时间"
                    min-width="130">				
				</el-table-column>
				<el-table-column
                    align="center"
                    label="创建时间"
                    min-width="150">
					<template slot-scope="scope">
						<span>{{
						(scope.row.create_time* 1000) | dateFormatter('YYYY-MM-DD hh:mm:ss')
						}}</span>
					</template>							
				</el-table-column>
				<el-table-column
                    align="center"
                    prop="arrive_time"
                    label="上牌时间"
                    min-width="150">
					<template slot-scope="scope">
						<span v-if="scope.row.arrive_time==0">----</span>
						<span v-else>{{
						(scope.row.arrive_time* 1000) | dateFormatter('YYYY-MM-DD hh:mm:ss')
						}}</span>						
					</template>
				</el-table-column>				
				<el-table-column
                    align="center"
                    prop="originate"
                    label="预约来源"
					v-if="!externalData"
                    min-width="100">
				</el-table-column>
				<el-table-column
                    align="center"
                    prop="state"
                    label="是否达到"
                    min-width="100">
				</el-table-column>
                <el-table-column
                    align="center"
                    label="操作"
                    min-width="140"
					v-if="!externalData"
					>
                    <template slot-scope="scope">
                        <el-button @click="lookOverGoodsDetail(scope.row.id)" type="primary">查看详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="bottom">
                <div class="btn-group">
                    <el-button size="mini" type="primary" class="btn-bottom" @click="expertData">导出</el-button>
                    <!-- <button type="button" class="btn-bottom" @click="showDeleteDialog">删除</button> -->
                </div>
                <!--分页部分-->
                <div class="paginationOne">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchContent.currentPage"
                            :page-sizes="[10, 20, 50, 100]"
                            :page-size="searchContent.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
        <el-dialog
                :before-close="handleClose"
                title="提示"
                :visible="dialogVisible"
                width="30%">
            <span>{{dialogMsg}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelUnshelve">取 消</el-button>
                <el-button type="primary" @click="confirmUnshelve">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
	import { resize } from "@/common/js/height.js";
	import { objScreen } from "@/common/js/util";
    import {dateFormatter} from "@/common/js/util"
	export default {
		props: {
            externalData: { // 注意此属性不要删除，当此组件被单独引入第三方组件时候的父组件传值属性
				type: Object,
				default: null
			} 
		},
		data() {
			return {
				// options: [
                //     {label: '维修', value: 1},
				// 	{label: '保养', value: 2},
				// 	{label: '试驾', value: 3},
				// 	{label: '上门保养', value: 4},
				// 	{label: '保养套餐', value: 5}
				// ],
				originateOptions:[
					{label: '小程序', value: 8}
				],
				statusOptions:[
					{label: '未到达', value: 1},
					{label: '已到达', value: 2},
					{label: '已取消', value: 3},
					// {label: '已完成', value: 3}
				],
				oHeight: 800,
                searchContent: {
					currentPage:1,
					pageSize:localStorage.getItem('pageSize')*1||10,
                	id: '',
	                address: '',
	                arrive_time: '',
	                mobile: '',
	                add_time: '',
					originate: '',
					state:'',
				},
				searchObj:{},
                goodsList: [],
				total: 0,
				needUnshelveGoods: null,
				dialogVisible: false,
				dialogMsg: ''
            }
        },
        methods: {
			getOptions(){
				//获取来源originateOptions
				// this.$http.post('/admin/appointment/index.php?act=appoint&op=getOriginates').then((res)=>{
				// 	if(res.code===1){
				// 		this.originateOptions = res.data||[];
				// 	}
				// })
				// //获取预约状态statusOptions
				// this.$http.post('/admin/appointment/index.php?act=appoint&op=getReservationStatus').then((res)=>{
				// 	if(res.code===1){
				// 		this.statusOptions = res.data||[];
				// 	}
				// })
				// //获取预约类型activeOptions
				// this.$http.post('/admin/appointment/index.php?act=appoint&op=getTypes').then((res)=>{
				// 	if(res.code===1){
				// 		this.options = res.data||[];
				// 	}
				// })
			},
			expertData(){
				if(this.$store.state.isServer){
					var host = location.host.replace('admin','api');
				}else{
					var host = location.host + '/api'
				}
				var url = location.protocol+"//"+host+'/admin/appointment/index.php?act=licensing&op=appointListExport';
				var searchData = this.searchObj;
				for(var key in searchData){
                    if(!(searchData[key]===null||searchData[key]===''||searchData[key]===undefined||key==='page'||key==='limit')){
						url+='&'+key+'='+searchData[key]
                    }
				}
				window.location.href = url;
			},
	        resetSearchData() {
		        this.searchContent = {
					currentPage:1,
					pageSize:localStorage.getItem('pageSize')*1||10,
					originate: this.searchContent.originate
				}
	        },
	        search() {
				this.searchContent.currentPage = 1;
				let arrive_time = this.searchContent.arrive_time;
				let add_time = this.searchContent.add_time;
				this.searchObj = objScreen({
					id: this.searchContent.id,
	                address: this.searchContent.address,
					start_licensing_time:arrive_time&&dateFormatter('YYYY-MM-DD 00:00:00', arrive_time[0]),
					end_licensing_time:arrive_time&&dateFormatter('YYYY-MM-DD 23:59:59', arrive_time[1]),
	                mobile: this.searchContent.mobile,
					start_create_time: add_time&&dateFormatter('YYYY-MM-DD 00:00:00', add_time[0]),
					end_create_time:add_time&&dateFormatter('YYYY-MM-DD 23:59:59', add_time[1]),
					originate: this.searchContent.originate,
					state:this.searchContent.state,
				})
		        this.getListData();
	        },
	        handleSizeChange(pageSize) {
				localStorage.setItem('pageSize',pageSize);
		        this.searchContent.pageSize = pageSize;
		        this.searchContent.currentPage = 1;
		        this.getListData();
            },
	        handleCurrentChange(page) {
		        this.searchContent.currentPage = page;
		        this.getListData();
            },
	        getListData() {
		        var data = this.searchObj;
				data.page = this.searchContent.currentPage;
				data.limit = this.searchContent.pageSize;
		        this.$http.post('/admin/appointment/index.php?act=licensing&op=list',data).then((res)=> {
			        if (res.code == 200) {
				        if (res.data && res.data.list) {
					        this.goodsList = res.data.list.map(function(item) {
						        return {
							        id: item.id,
							        storeName: item.store_name,
							        arrive_time: item.arrive_time,
                                    create_time: item.create_time,
                                    licensing_time: item.licensing_time,
							        originate:item.originate==8?'小程序':'其他',
									state:item.state==1?'未到达':item.state==2?'已到达':'已取消',
									store_id:item.store_id,
									address:item.address,
						        }
							});
					        this.total = parseInt(res.data.count);
				        } else {
					        this.goodsList = [];
					        this.total = 0;
				        }
			        }
			        else if (res.code == 2001) {
				        this.goodsList = [];
				        this.total = 0;
			        }
			        else {
				        alert(res.msg);
			        }
		        })
	        },
	        unshelveGoods(goodsItem) {
		        this.$http.post('/admin/preferential_car/index.php?act=preferentialCar&op=pullOffShelves', {
			        id: goodsItem.id
		        }).then(res => {
			        if (res.code == 1) {
				        goodsItem.status = 2
			        } else {
				        alert(res.msg)
			        }
		        })
            },
	        handleClose() {
		        this.cancelUnshelve();
	        },
	        cancelUnshelve() {
		        this.dialogVisible = false;
		        this.dialogMsg = '';
		        this.needUnshelveGoods = null;
	        },
	        confirmUnshelve() {
		        this.unshelveGoods(this.needUnshelveGoods);
		        this.cancelUnshelve();
	        },
	        lookOverGoodsDetail(id) {
	        	this.$router.push(`/home/operate-panel/licensing-list/manadetail?id=${id}`)
            }
        },
        mounted() {
	        resize(this)
        },
		created() {
			if(this.externalData) {
				if (this.externalData.originate) {
					this.searchContent.originate = this.externalData.originate;
					this.searchObj.originate = this.externalData.originate;
				}
				if (this.externalData.filter_not_yt) {
					this.searchContent.filter_not_yt = this.externalData.filter_not_yt;
					this.searchObj.filter_not_yt = this.externalData.filter_not_yt;
				}
			}
			this.$parent.tabLists = [
				{"label": "预约管理", "name": 'management'}
			]
			this.getOptions();
			this.getListData();
			
		},
		name: "management"
	}
</script>

<style lang="scss" scoped>
    .on-sale-good-car {
        .search-content {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            &-input {
                width: 200px;
            }
            .putaway-time-wrapper {
                .putaway-time-label {
                    font-size: 14px;
                }
                display: flex;
                align-items: center;
                /deep/.el-range-separator {
                    box-sizing: content-box;
                }
            }
        }
    }
	.bottom{
		display: flex;
		justify-content:space-between;
		align-items: center;
	}
</style>